package com.youyou.fusheng2.View.Fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.youyou.fusheng2.Moudle.FuShengCallBack;
import com.youyou.fusheng2.Moudle.OnClick2;
import com.youyou.fusheng2.Moudle.SouSuoImageCliCk;
import com.youyou.fusheng2.Moudle.showText;
import com.youyou.fusheng2.Person.FuShengMoudle;
import com.youyou.fusheng2.Person.Test.FenLeiBomTest;
import com.youyou.fusheng2.Person.Test.FenLeiTopLeixingTest;
import com.youyou.fusheng2.R;
import com.youyou.fusheng2.Utils.EveryUrl;
import com.youyou.fusheng2.View.Adapter.FenLeiBottomAdapter;
import com.youyou.fusheng2.View.Adapter.FenleiTopAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FenleiFragment extends Fragment implements FuShengCallBack, showText, OnLoadMoreListener, View.OnClickListener, OnClick2 {

    private RecyclerView              fenlei_recBoottom;
    private RecyclerView              fenlei_rectop;
    private TextView                  fenlei_show;
    private TextView                  home_tetle;
    private ImageView                 home_sousuo;
    //自定义Hander
    private MyHander                  myHander      = new MyHander();
    //上面三个横向标签的集合
    private List<String>              leixingList   = new ArrayList<>();
    private List<String>              diquList      = new ArrayList<>();
    private List<String>              yearsList     = new ArrayList<>();
    //每个电影的集合
    private List<Map<String, String>> ImageItem     = new ArrayList<>();
    private Map<String, String>       mapItem;
    private FenLeiBottomAdapter       fenLeiBottomAdapter;
    //默认的回调数据
    private String                    s1            = "0";
    private String                    s2            = "0";
    private String                    s3            = "0";
    //刷新的index和 判断设置到适配器的参数ool 如果是0就进行初始化适配器 否则就直接设置到适配器
    private int                       index         = 5;
    private int                       ool           = 0;
    private SmartRefreshLayout        fenlei_Refresh;
    private FuShengMoudle             fuShengMoudle = new FuShengMoudle((FuShengCallBack) this);

    private SouSuoImageCliCk souSuoImageCliCk;

    public void setOnClickListner(SouSuoImageCliCk souSuoImageCliCk) {
        this.souSuoImageCliCk = souSuoImageCliCk;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fenlei_fragment, null);
        initView(inflate);
        GetTabs();
        //进行请求下面的图片和文字
        GetTopList(this.s1, this.s2, this.s3);
        //初始化集合数据
        addList();
        //初始化上面的rec
        setDateTop();
        return inflate;
    }

    private void addList() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:39
         *  @简介: 初始化剩下两个集合的数据
         */
        diquList.add("全部");
        diquList.add("大陆");
        diquList.add("香港");
        diquList.add("欧美");
        diquList.add("泰国");
        diquList.add("台湾");
        diquList.add("日本");
        yearsList.add("全部");
        yearsList.add("2020");
        yearsList.add("2019");
        yearsList.add("2018");
        yearsList.add("2017");
        yearsList.add("2016");
        yearsList.add("2015");
    }

    private void GetTabs() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:09
         *  @简介: 请求电影分类
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                fuShengMoudle.getUrlDate(EveryUrl.vdioFenlei, 0);
            }
        }).start();
    }

    private void initView(View inflate) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/23  18:44
         *  @简介: 初始化布局
         */
        fenlei_rectop = inflate.findViewById(R.id.fenlei_recTop);
        home_sousuo = inflate.findViewById(R.id.home_sousuo);
        home_sousuo.setOnClickListener(this);
        fenlei_recBoottom = inflate.findViewById(R.id.fenlei_recBoottom);
        fenlei_Refresh = inflate.findViewById(R.id.fenlei_Refresh);
        fenlei_Refresh.setOnLoadMoreListener(this);
        fenlei_Refresh.setEnableRefresh(false);
        fenlei_Refresh.setEnableOverScrollDrag(true);
        fenlei_Refresh.setEnableOverScrollBounce(true);
        fenlei_Refresh.setEnableFooterTranslationContent(true);
        fenlei_Refresh.setEnableAutoLoadMore(true);
        fenlei_show = inflate.findViewById(R.id.fenlei_show);
        home_tetle = inflate.findViewById(R.id.home_tetle);
        home_tetle.setText("分类");
    }

    @Override
    public void GetSuccess(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:16
         *  @简介: 把请求的数据发送到MyHaner
         *         请求成功
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @Override
    public void GetError(String text, int mark) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:16
         *  @简介: 把请求失败的信息发送到MyHander
         */
        Message message = new Message();
        message.what = mark;
        message.obj = text;
        myHander.sendMessage(message);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void showText(String s1, String s2, String s3) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  02:49
         *  @简介: 接口返回的数据 分类标签下的数据
         */
        //在进行点击其他标签的时候 清理所有此页面的集合数据和所有rec的item
        ImageItem.clear();
        fenlei_recBoottom.removeAllViews();
        fenlei_show.setText((s1.equals("0") ? "全部" : s1) + " · " + (s2.equals("0") ? "全部" : s2) + " · " + (s3.equals("0") ? "全部" : s3));
        for (int i = 0; i < leixingList.size(); i++) {
            if (leixingList.get(i).equals(s1)) {
                this.s1 = String.valueOf(i);
            }
        }
        //把回掉的数据设置到全局变量 防止点击其他标签 其他两个变量同时更改
        this.s2 = s2;
        this.s3 = s3;
        this.ool = 0;
        //点击标签一次 进行多参数请求一次
        GetTopList(this.s1, this.s2, this.s3);
    }

    private void GetTopList(String s1, String s2, String s3) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  19:34
         *  @简介: 进行电影分类 多参数请求 此33是伦理片 所以为了更加精确 用三木判断一下
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                fuShengMoudle.getUrlDate(EveryUrl.vdioFenleiItem + (s1.equals("33") ? 33 + 1 : s1) + "/" + (s2.equals("全部") ? "0" : s2) + "/" + (s3.equals("全部") ? "0" : s3) + "/" + 0 + "/" + index, 1);
            }
        }).start();
    }

    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  19:36
         *  @简介: 进行上拉刷新
         *  index：多参数下标 没此上拉 进行 设置+=18
         *  因为没次返回18条数据
         */
        this.index += 18;
        GetTopList(this.s1, this.s2, this.s3);
        //延时一秒后结束刷新
        fenlei_Refresh.finishLoadMore(1000);
    }

    @Override
    public void onClick(View view) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/17  19:39
         *  @简介: 控件监听事件
         */
        if (view.getId() == R.id.home_sousuo) {
            if (souSuoImageCliCk != null) {
                souSuoImageCliCk.OnImageCliCk(home_sousuo);
            }
        }
    }

    @Override
    public void listener1(int Flage, String Text) {
        Bundle bundle = new Bundle();
        bundle.putInt("f", Integer.parseInt(Text));
        getContext().startActivity(new Intent(getContext(), com.youyou.fusheng2.View.Activity.playVdioActivity.class).putExtra("text", bundle));
    }

    @Override
    public void listener1(int Flage, String Text, int Index) {

    }


    public class MyHander extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    jiexiTopTabs(msg.obj.toString());
                    break;
                case 1:
                    jiexiBomImage(msg.obj.toString());
                    if (ool == 1) {
                        fenLeiBottomAdapter.setItemList(ImageItem);
                        fenLeiBottomAdapter.notifyDataSetChanged();
                    } else {
                        setDateBom();
                    }
                    break;
                default:
                    Toast.makeText(getContext(), msg.obj.toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void jiexiBomImage(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  19:38
         *  @简介: 解析每个标签的数据
         */
        Gson gson = new Gson();
        FenLeiBomTest fenLeiBomTest = gson.fromJson(toString, FenLeiBomTest.class);
        List<FenLeiBomTest.DataBean> data = fenLeiBomTest.getData();
        if (data.size() != 0) {
            for (int i = 0; i < data.size(); i++) {
                mapItem = new HashMap<>();
                mapItem.put("tid", String.valueOf(data.get(i).getV_id()));
                mapItem.put("tname", data.get(i).getV_name());
                mapItem.put("tpic", data.get(i).getV_pic());
                mapItem.put("tnote", String.valueOf(data.get(i).getV_score()));
                ImageItem.add(mapItem);
            }
        } else {
            Toast.makeText(getContext(), "没有数据", Toast.LENGTH_SHORT).show();
        }

    }

    private void jiexiTopTabs(String toString) {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:33
         *  @简介: 解析顶部第一行的类别
         */
        Gson gson = new Gson();
        FenLeiTopLeixingTest fenLeiTopLeixingTest = gson.fromJson(toString, FenLeiTopLeixingTest.class);
        List<FenLeiTopLeixingTest.DataBean> data = fenLeiTopLeixingTest.getData();
        leixingList.add("全部");
        for (int i = 0; i < data.size(); i++) {
            String tname = data.get(i).getTname();
            leixingList.add(tname);
        }
    }

    private void setDateTop() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:20
         *  @简介: 把数据设置到适配器
         */
        FenleiTopAdapter fenleiTopAdapter = new FenleiTopAdapter(leixingList, diquList, yearsList, (showText) this);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(), RecyclerView.VERTICAL, false);
        fenlei_rectop.setAdapter(fenleiTopAdapter);
        fenlei_rectop.setLayoutManager(layoutManager);
        fenleiTopAdapter.notifyDataSetChanged();
    }


    private void setDateBom() {
        /**
         *  @作者: $浮生-qq:2567294605$
         *  @时间: 2020/8/24  01:20
         *  @简介: 把数据设置到适配器
         */
        fenLeiBottomAdapter = new FenLeiBottomAdapter(ImageItem,this);
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        fenlei_recBoottom.setAdapter(fenLeiBottomAdapter);
        fenlei_recBoottom.setLayoutManager(layoutManager);
        fenLeiBottomAdapter.notifyDataSetChanged();
        //初始化适配器的时候 把全局参数设置为1 防止上拉刷新 页面置顶
        this.ool = 1;
    }
}